<template>
  <div class="change-class body-bg">
    <div v-if="!initLoading">
      <ul class="change-content" v-if="list.length">
        <li class="sign-items" v-for="(item,index) in list" :key="index" @click="goDet(item.goods_id,item.package_discount,item.open_date,item.end_date)">
          <div class="items-top">
            <div class="teacher-infor">
              <img :src="item.teacher_photo || '/static/images/user.png'" alt="头像">
              <span>{{item.teacher_name}}</span>
            </div>
            <div>
              <p class="discount-price">￥{{item.mall_cost}}</p>
              <p class="price">￥{{item.cost || item.activity_cost}}</p>
            </div>
          </div>
          <div class="item-cont">
            <img v-if="item.package_discount == '1'" class="taocan" src="/static/images/singUp/singUp-icon4.png" alt="套">
            <p class="class-name overflow-one">{{item.name}}</p>
            <p class="class-infor"><img src="/static/images/singUp/singUp-icon1.png" alt="icon">{{item.campus_name}}</p>
            <p class="class-infor"><img src="/static/images/singUp/singUp-icon2.png" alt="icon">{{item.open_date}} 至 {{item.end_date}}</p>
            <p class="class-infor"><img src="/static/images/singUp/singUp-icon3.png" alt="icon">{{item.goods_week}}</p>
            <img v-if="item.isTaoCan == '1'" class="taocan" src="/static/images/singUp/singUp-icon4.png" alt="套餐">
          </div>
          <div class="items-tips">
            <span class="items-tip-biaoqie" v-if="item.is_transfer == '1'">随时插班</span>
            <span class="items-tip-biaoqie" v-if="item.retreat_rule == '1'">随时退班</span>
            <span class="items-tip-biaoqie" v-if="item.is_audition == '1'">免费试听</span>
          </div>
        </li>
        <li v-if="isReachBottom" class="pull-loading"><van-loading type="spinner" size="20px" color="#1989fa" /></li>
        <li v-if="isBottom" class="bottom-line">-- 亲，已经到最后了--</li>
      </ul>
      <div v-else class="empty">
        <img src="/static/images/empty-icon.png" alt="空">
        <p class="empty-tips">暂无数据哦</p>
      </div>
    </div>
    <div v-else class="init-loading"><van-loading type="spinner" color="#1989fa" /></div>
  </div>
</template>

<script>
import store from '../../store/globalStore'
import Ls from '@/utils/storage'
export default {
  data() {
    return {
      from: "", // 记录路由来源 1 报名 2 续费
      initLoading: true, // 初始加载
      isBottom: false, // 是否加载完全部数据
      isReachBottom: false, // 是否显示加载状态
      class_hour_minute: "", // 课时
      campus_id: "", // 校区id
      renew_goods_id: "", // 续报班级id
      season_id: "", // 季节id
      curr_page: 1,
      page_size: 10,
      total: "",
      isXuFei: false,
      list: [
        {isTaoCan: "1", headImg: "https://img.yzcdn.cn/vant/cat.jpeg", price:"199", disPrice: "120", title: "春季半年-90分钟课程班", week: "周四上课", teacherName: '李响', dateTime:'2019-12-12', address: '北京海淀黄庄校区北京海淀黄庄校区'},
        {isTaoCan: "2", headImg: "https://img.yzcdn.cn/vant/cat.jpeg", price:"199", disPrice: "120", title: "春季半年-90分钟课程班", week: "周四上课", teacherName: '李响', dateTime:'2019-12-12', address: '北京海淀黄庄校区北京海淀黄庄校区'},
        {isTaoCan: "2", headImg: "https://img.yzcdn.cn/vant/cat.jpeg", price:"199", disPrice: "120", title: "春季半年-90分钟课程班", week: "周四上课", teacherName: '李响', dateTime:'2019-12-12', address: '北京海淀黄庄校区北京海淀黄庄校区'},
        {isTaoCan: "1", headImg: "https://img.yzcdn.cn/vant/cat.jpeg", price:"199", disPrice: "120", title: "春季半年-90分钟课程班", week: "周四上课", teacherName: '李响', dateTime:'2019-12-12', address: '北京海淀黄庄校区北京海淀黄庄校区'},
        {isTaoCan: "1", headImg: "https://img.yzcdn.cn/vant/cat.jpeg", price:"199", disPrice: "120", title: "春季半年-90分钟课程班", week: "周四上课", teacherName: '李响', dateTime:'2019-12-12', address: '北京海淀黄庄校区北京海淀黄庄校区'},
      ]
    }
  },
  onLoad(e) {
    // url中传递了from参数，1 来自报名 2 来自续费 (续费时需要传递 class_hour_minute 课时 season_id 季节id, renew_goods_id 续报班级id)
    console.log(e)
    this.from = e.from;
    if(e.from == "1"){
      this.isXuFei = false;
      this.class_hour_minute = "";
      this.season_id = "";
      wx.setNavigationBarTitle({
        title: '报名咨询'
      })
    } else {
      this.isXuFei = true;
      this.class_hour_minute = e.class_hour_minute;
      this.season_id = e.season_id;
      this.renew_goods_id = e.renew_goods_id;
      wx.setNavigationBarTitle({
        title: '班级续费'
      })
    }
  },
  onShow() {
    this.campus_id = Ls.getItem("childInfor").student_school_id;
    this.curr_page = 1;
    this.isBottom = false;
    this.getGoodsList();
  },
  onUnload() {
    this.initLoading = true;
  },
  // 下拉刷新
  onPullDownRefresh: function() {
    this.curr_page = 1;
    this.isReachBottom = false;
    this.isBottom = false;
    this.getGoodsList();
  },
  // 上拉加载
  onReachBottom () {
    if(this.isBottom) return;
    this.isReachBottom = true;
    this.getGoodsList();
  },
  methods: {
    // 获取列表
    getGoodsList() {
      wx.showNavigationBarLoading() //在标题栏中显示加载
      if(this.isReachBottom) {
        this.curr_page++;
      }
      this.$fetch.getGoodsList({
          campus_id: this.campus_id,
          class_hour_minute: this.class_hour_minute,
          season_id: this.season_id,
          curr_page: this.curr_page,
          page_size: this.page_size
      }).then(res=>{
        wx.hideNavigationBarLoading() //在标题栏中隐藏加载
        wx.stopPullDownRefresh(); //完成停止加载
        this.initLoading = false;
        this.total = res.result.total;
        let len = 0;
        if(this.isReachBottom) { // 判断是下拉还是上拉动作
          this.list = [...this.list, ...res.result.data];
          len = this.list.length;
          if(len >= this.total){
            this.isBottom = true;
          }
          this.isReachBottom = false;
          return
        }
        this.list = res.result.data;
        len = this.list.length;
        if(len >= this.total){
          this.isBottom = true;
        }
      }).catch(err=>{
        this.initLoading = false;
        this.isReachBottom = false;
        wx.hideNavigationBarLoading() //在标题栏中隐藏加载
        wx.stopPullDownRefresh(); //完成停止加载
      })
    },
    // 详情
    goDet(goods_id,isTaoCan,open_date,end_date,renew_goods_id) {
      let that = this;
      wx.navigateTo({
        url: `/pages/signUpDet/main?from=${that.from}&goods_id=${goods_id}&isTaoCan=${isTaoCan}&open_date=${open_date}&end_date=${end_date}&renew_goods_id=${that.renew_goods_id}`
      })
    }
  },
}
</script>

<style lang="less" scope>
.change-class{
  width: 100%;
  min-height: 100vh;
  .change-content{
    width: 100%;
    padding-top: 14px;
    .sign-items{
      width: 100%;
      background: #fff;
      overflow: hidden;
      margin-bottom: 14px;
      .items-top{
        width: 100%;
        padding: 8px 20px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .teacher-infor{
          display: flex;
          justify-content: flex-start;
          align-items: center;
          img{
            width: 44px;
            height: 44px;
            display: block;
            border-radius: 50%;
          }
          span{
            margin-left: 10px;
            font-size: 14px;
            font-weight: bold;
            color: #000;
          }
        }
        .price{
          text-decoration:line-through;
          color: #A9A9A9;
          font-size: 14px;
          font-weight: bold;
        }
        .discount-price{
          color: #EE0000;
          font-size: 14px;
          font-weight: bold;
        }
      }
      .item-cont{
        width: 100%;
        background: #E5F9FF;
        padding: 14px 22px;
        box-sizing: border-box;
        position: relative;
        .taocan{
          position: absolute;
          top: 6px;
          right: 12px;
          width: 24px;
          height: 24px;
          display: block;
        }
        .class-name{
          width: 100%;
          font-size: 14px;
          color: #666;
          font-weight: bold;
        }
        .class-infor{
          width: 100%;
          font-size: 12px;
          color: #999;
          display: flex;
          justify-content: flex-start;
          align-items: center;
          margin-top: 10px;
          img{
            width: 12px;
            height: 12px;
            display: block;
            margin-right: 6px;
          }
        }
      }
      .items-tips{
        width: 100%;
        padding: 10px 12px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        .items-tip-biaoqie{
          padding: 2px 8px;
          border: 1px solid rgba(0,183,238,1);
          border-radius: 8px;
          font-size: 12px;
          color: #00B7EE;
          margin-left: 12px;
        }
      }
    }
  }
}
</style>
